/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package com.mxgraph.examples.swing.editor;

import com.mxgraph.view.mxGraph;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.snmp4j.CommunityTarget;
import org.snmp4j.Snmp;
import org.snmp4j.TransportMapping;
import org.snmp4j.mp.SnmpConstants;
import org.snmp4j.smi.Address;
import org.snmp4j.smi.GenericAddress;
import org.snmp4j.smi.OID;
import org.snmp4j.smi.OctetString;
import org.snmp4j.smi.VariableBinding;
import org.snmp4j.transport.DefaultUdpTransportMapping;
import org.snmp4j.util.DefaultPDUFactory;
import org.snmp4j.util.TreeEvent;
import org.snmp4j.util.TreeUtils;

/**
 *
 * @author somesh
 */
public class SnmpWalk {
   //String targetIP;
    				String targetAddr;
				String oidStr;
				String commStr;
                                int snmpVersion;
    public List<String> snmpWalkOutput = new ArrayList<String>();
    public SnmpWalk(String targetAddr, String oidStr,String commStr,int snmpVersion){
    this.targetAddr = targetAddr;
    this.oidStr = oidStr;
    this.commStr = commStr;
    this.snmpVersion = snmpVersion;}
  		public List<String> doSnmpwalk() {
			// public ArrayList<String> doSnmpwalk(String ip, int i) throws
			// IOException{
			try {
				

				//int snmpVersion;
				//String portNum;
				//String usage;
				//String PortNumb[];
				//String ConnectedSwitch[];
				//targetAddr = ip;
				//String IPConnectedSwitch;
				// targetAddr ="10.250.250.81";
				//oidStr = "1.0.8802.1.1.2.1.4.2.1.3";
				//commStr = "d@ere@d";
				//snmpVersion = SnmpConstants.version2c;
				//portNum = "161";

				// InetAddress.getByName(String ip);
				// System.out.println("1");
				Address targetAddress = GenericAddress.parse("udp:"
						+ targetAddr + "/161");
				TransportMapping transport = new DefaultUdpTransportMapping();
				// System.out.println("2");
				Snmp snmp = new Snmp(transport);
				transport.listen();
				CommunityTarget target = new CommunityTarget();
				target.setCommunity(new OctetString(commStr));

				target.setAddress(targetAddress);
				target.setRetries(1);
				target.setTimeout(1000);
				target.setVersion(snmpVersion);
				// System.out.println("1");
				OID oid = null;
				try {
					oid = new OID(oidStr);
				} catch (RuntimeException ex) {
				//	System.out.println("OID is not specified correctly.");
					System.exit(1);
				}
				TreeUtils treeUtils = new TreeUtils(snmp,
						new DefaultPDUFactory());
				// System.out.println(target);
				// System.out.println(oid);
				List<TreeEvent> events = treeUtils.getSubtree(target, oid);
				if (events == null || events.size() == 0) {
					System.exit(1);
				}
				for (TreeEvent event : events) {
					// System.out.println("7");
					if (event != null) {
						if (event.isError()) {
							System.err.println("oid [" + oid + "] "
									+ event.getErrorMessage());
						}
						VariableBinding[] varBindings = event
								.getVariableBindings();
						if (varBindings == null || varBindings.length == 0) {
							//System.out.println("No result returned.");
						}
						int connectedDevicesCounter = 1;
						for (VariableBinding varBinding : varBindings) {
							String stringFormOID = varBinding.getOid().format();
snmpWalkOutput.add(stringFormOID);
							}
							//break;
						}
					}
				//}
				snmp.close();

			} catch (Exception exceptionSnmpQuery) {
                            	System.out.println("accident");
				System.out.println(exceptionSnmpQuery);
			}

			// return checkIP;
		return snmpWalkOutput;}
	}
  
//}
